import type { BanquetView, LayoutDefine, LayoutElement } from "@/types/banquet";
import request from "@/utils/http";

/**
 * 把后端平铺 elements 转成二维网格
 * @param elements
 * @param persons
 * @param layout
 * @returns
 */
export function buildSeatGrid(elements: LayoutElement[], layout: LayoutDefine): LayoutElement[][] {
  // 用索引填充数组
  // Array.from({length: 5}, (v, i) => i); // [0, 1, 2, 3, 4]
  const grid: LayoutElement[][] = Array.from({ length: layout.rowCount }, (_, r) =>
    Array.from({ length: layout.colCount }, (_, c) => ({
      uuid: null,
      rowIndex: r + 1,
      colIndex: c + 1,
      active: false,
      type: layout.type,
    }))
  );

  elements.forEach((s) => {
    const r = s.rowIndex - 1;
    const c = s.colIndex - 1;
    if (grid[r] && grid[r][c]) {
      grid[r][c] = { ...s };
    }
  });
  return grid;
}

export async function fetchSeatSchedule(name: string): Promise<any> {
  const { data } = await request<any>({
    url: "/api/weapp/banquet/seat/schedule",
    params: { personName: name },
    method: "GET",
  });
  return data;
}

export async function fetchSeat(orderDetailId: number, personId: number): Promise<BanquetView> {
  const { data } = await request<BanquetView>({
    url: "/api/weapp/banquet/seat",
    params: { orderDetailId, personId },
    method: "GET",
  });
  return data;
}
